package com.example.demo3.controller;


import com.dsbj.common.msg.DataPageMessage;
import com.dsbj.common.msg.ResponseMessage;
import com.example.demo3.entity.CodeReason;
import com.example.demo3.entity.DictData;
import com.example.demo3.service.CodeReasonService;
import com.example.demo3.service.DictTypeService;
import com.example.demo3.utils.constant.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 原因码 前端控制器
 * </p>
 *
 * @author xsyuan
 * @since 2020-03-27
 */
@Controller
@RequestMapping("codeReason")
public class CodeReasonController extends BaseController {

    @Resource
    private DictTypeService dictTypeService;

    @Resource
    private CodeReasonService codeReasonService;


    @RequestMapping("list")
    public void list(){}

    @RequestMapping("codeReasonPageInfo")
    @ResponseBody
    public DataPageMessage codeReasonPageInfo(int pageindex, String orderdir, int pagesize, String ordercolumn, int sEcho, String textVague, String codeVague, String nameVague){
        Map<Object,Object> map = new HashMap<>();
            map.put("pageindex",pageindex);
            map.put("pagesize",pagesize);
            map.put("orderdir",orderdir);
            map.put("ordercolumn",ordercolumn);
        if(StringUtils.isNotBlank(textVague)){
            map.put("textVague",textVague);
        }
        if(StringUtils.isNotBlank(codeVague)){
            map.put("codeVague",codeVague);
        }
        if(StringUtils.isNotBlank(nameVague)){
            map.put("nameVague",nameVague);
        }
        int countPage=codeReasonService.countPage(map);
        List<CodeReason> codeReasonList=codeReasonService.queryPage(map);
        return DataPageMessage.ok(codeReasonList,countPage,countPage,sEcho,"");
    }

    @RequestMapping("add")
    public String add (Model model){
        List<DictData> dictDataList =dictTypeService.getByType(Constants.DICT_TYPE_DTC_REASON_TYPE);
        model.addAttribute("dictDataList",dictDataList);
        return "codeReason/add";
    }

    @RequestMapping("edit")
    public String edit (Model model, String id){
        if(StringUtils.isNotBlank(id)){
           Map<String,Object> map  = new HashMap<>();
                map.put("id",id);
                map.put("delFlag",0);
            List<CodeReason> codeReasonList = codeReasonService.getByMap(map);
            if(codeReasonList.size() >0 && codeReasonList != null && !codeReasonList.isEmpty()){
                model.addAttribute("codeReason",codeReasonList.get(0));
            }
        }
        return "codeReason/edit";
    }

    @RequestMapping("del")
    @ResponseBody
    public ResponseMessage del (CodeReason codeDtcList){
        codeDtcList.setDelFlag(1);
        codeReasonService.update(codeDtcList);
        return ResponseMessage.ok("删除成功");
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public ResponseMessage saveOrUpdate(CodeReason codeDtcList){
        codeDtcList.setCreateUser("xsyuan");
        codeDtcList.setCreateTime(new Date());
        String msg= "";
        if(StringUtils.isNotBlank(codeDtcList.getId())){
            codeDtcList.setUpdateUser("xsyuan");
            codeDtcList.setUpdateTime(new Date());
            codeReasonService.update(codeDtcList);
            msg = "修改成功";
        }else{
            codeDtcList.setCreateUser("xsyuan");
            codeDtcList.setCreateTime(new Date());
            codeReasonService.insert(codeDtcList);
            msg = "添加成功";
        }
        return ResponseMessage.ok(msg);
    }

}
